//
//  XWTabBar.swift
//  新浪 weibo
//
//  Created by xuwei on 16/1/19.
//  Copyright © 2016年 xuwei. All rights reserved.
//

import UIKit

class XWTabBar: UITabBar {
    
    var composeButtonClickClourse: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(composeButton)
        backgroundImage = UIImage(named: "tabbar_backgroud")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center = CGPoint(x: frame.width*0.5, y: frame.height*0.5)
        
        let childW : CGFloat = frame.width*0.2
        
        var index = 0
        
        for childView in self.subviews{
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                childView.frame.size.width = childW
                childView.frame.origin.x = CGFloat(index)*childW
                
                index++
                
                
                if index==2{
                    index++
                }
            }
        }
    }
    
    
    
    @objc private func composeButtonClick(){
        composeButtonClickClourse?()
    }
    
    
    private lazy var composeButton : UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button .sizeToFit()
        
        
        return button
    }()
}
